<template>
  <div
    class="customer-fourth-page"
    v-loading="loading"
    element-loading-text="加载中..."
  >
    <div class="customer-fourth-title">
      <div class="customer-fourth-title-left">
        <span>标签设置</span>
      </div>
      <div class="customer-fourth-title-right">
        <el-button type="primary" @click="addDeptFun('add')">添加</el-button>
      </div>
    </div>
    <div class="customer-fourth-table">
      <el-table
        border
        stripe
        :data="tableData"
        :height="store.tableHeight - 60"
      >
        <el-table-column prop="name" label="标签组名称" width="200" />
        <el-table-column label="标签">
          <template #default="{ row }">
            <div v-if="row.children && row.children.length > 0">
              <el-tag
                v-for="item in row.children"
                :style="{
                  color: row.text_color,
                  background: row.background_color,
                  borderColor: row.border_color,
                  marginRight: '10px',
                }"
                >{{ item.name }}</el-tag
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="190">
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              @click="updateDeptFun('update', row)"
            >
              编辑
            </el-button>
            <el-button type="success" link @click="labelFun(row)">
              标签设置
            </el-button>
            <el-button type="danger" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <addDialog
    :treeData="tableData"
    ref="deptAddRef"
    @refreshList="refreshList"
  />
  <labelDialog ref="labelDialogRef" @refreshList="refreshList" />
</template>
<script setup>
import { useCounterStore } from "@/stores/counter.js";
import { listApi, delApi } from "@/plugins/api/customer/setup/fourth";
import addDialog from "./dialog/DialogFourth.vue";
import labelDialog from "./dialog/DialogFourth-label.vue";
const store = useCounterStore();
let tableData = ref([]);
let loading = ref(false);
function getData() {
  loading.value = true;
  listApi()
    .then((data) => {
      tableData.value = data;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}
let deptAddRef = ref(null);
function addDeptFun(type) {
  deptAddRef.value.initFun(type);
}
function updateDeptFun(type, row) {
  deptAddRef.value.initFun(type, row);
}
function refreshList() {
  getData();
}
function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}
// 标签设置
let labelDialogRef = ref(null);
function labelFun(row) {
  labelDialogRef.value.initFun(row);
}
onMounted(() => {
  getData();
});
</script>
<style scoped lang="scss">
.customer-fourth-page {
  width: 100%;
  height: 100%;

  .customer-fourth-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .customer-fourth-title-left {
      font-size: 18px;
      font-weight: 700;
      color: #172b4d;

      img {
        vertical-align: middle;
      }

      span {
        vertical-align: middle;
        margin-left: 5px;
      }
    }
  }

  .customer-fourth-table {
    padding: 10px;
    box-sizing: border-box;
  }
}
</style>
